home *** CD-ROM | disk | FTP | other *** search
- /***
- * CADSP
- *
- * AppleTalk Data Stream Protocol handler
- *
- * this object handles data stream connections over appletalk
- *
- * Copyright © 1992 Bernard Bernstein. All rights reserved.
- ***/
-
-
- #include <Global.h>
-
- #include "CADSP.h"
-
-
- /***
- * IADSP
- *
- * Initialize the ADSP handler.
- *
- * queueSize - size of the internal in/out queues
- * dataSize - size of the external queues
- *
- ***/
- void CADSP::IADSP(short queueSize, short dataSize)
- {
- short mppRefNum;
- short dspRefNum;
- Ptr sendQ = nil;
- Ptr recvQ = nil;
- Ptr dspattnBuf = nil;
- Ptr inBuf = nil;
- Ptr outBuf = nil;
- Ptr attnBuf = nil;
- DSPPBPtr dsppb = nil;
- TPCCB ccb = nil;
-
- initialized = false;
- opened = false;
-
- FailOSErr(OpenDriver("\p.MPP", &mppRefNum));
- FailOSErr(OpenDriver("\p.DSP", &dspRefNum));
-
- itsDspRefNum = dspRefNum;
- itsQueueSize = queueSize;
-
- TRY
- {
- // make non-relocatable blocks for ADSP internal use
- sendQ = NewPtr(queueSize);
- FailNIL(sendQ);
- recvQ = NewPtr(queueSize);
- FailNIL(recvQ);
- dspattnBuf = NewPtr(attnBufSize);
- FailNIL(dspattnBuf);
-
- ccb = (TPCCB)NewPtr(sizeof(TRCCB));
- FailNIL(ccb);
-
-
- // make non-relocatable blocks for my buffers
- inBuf = NewPtr(dataSize);
- FailNIL(inBuf);
- outBuf = NewPtr(dataSize);
- FailNIL(outBuf);
- attnBuf = NewPtr(dataSize);
- FailNIL(attnBuf);
-
-
- // make my parameter block non-relocatable
- dsppb = (DSPPBPtr)NewPtr(sizeof(DSPParamBlock));
- FailNIL(dsppb);
-
-
- // remember all that stuff so we can deallocate it when disposed
- dspRecvQ = recvQ;
- dspSendQ = sendQ;
- dspAttnBuf = dspattnBuf;
- itsCcb = ccb;
-
- itsInBuf = inBuf;
- itsOutBuf = outBuf;
- itsAttnBuf = attnBuf;
-
- itsDsppb = dsppb;
- }
- CATCH
- {
- ForgetPtr(sendQ);
- ForgetPtr(recvQ);
- ForgetPtr(dspattnBuf);
- ForgetPtr(inBuf);
- ForgetPtr(outBuf);
- ForgetPtr(attnBuf);
- ForgetPtr(dsppb);
- ForgetPtr(ccb);
- }
- ENDTRY;
- }
-
-
- /***
- * Dispose
- *
- * Kill pointers for ATP
- ***/
- void CADSP::Dispose(void)
- {
- DSPRemove();
-
- ForgetPtr(dspRecvQ);
- ForgetPtr(dspSendQ);
- ForgetPtr(dspAttnBuf);
- ForgetPtr(itsInBuf);
- ForgetPtr(itsOutBuf);
- ForgetPtr(itsAttnBuf);
- ForgetPtr(itsCcb);
- ForgetPtr(itsDsppb);
-
- inherited::Dispose();
- }
-
-
-
-
- /**********************************************************************
-
- Accessing connection information
-
- **********************************************************************/
-
-
- /***
- * DSPioResult
- *
- * The current ioResult. Needed when waiting for asynchronous
- * calls to complete.
- ***/
- short CADSP::DSPioResult(void)
- {
- return itsDsppb->ioResult;
- }
-
-
-
- /**********************************************************************
-
- Establishing and Terminating an ADSP connection
-
- **********************************************************************/
-
-
- /***
- * DSPInit
- *
- * Setup the ADSPConnector
- ***/
- void CADSP::DSPInit(short *socket)
- {
- // setup dspInit parameters
- itsDsppb->ioNamePtr = nil;
- itsDsppb->ioCRefNum = itsDspRefNum;
- itsDsppb->ioCompletion = nil;
- itsDsppb->ccbRefNum = 0;
- itsDsppb->csCode = dspInit;
-
- itsDsppb->u.initParams.ccbPtr = itsCcb;
- itsDsppb->u.initParams.userRoutine = nil;
- itsDsppb->u.initParams.sendQSize = itsQueueSize;
- itsDsppb->u.initParams.recvQSize = itsQueueSize;
- itsDsppb->u.initParams.sendQueue = (uPtr)dspSendQ;
- itsDsppb->u.initParams.recvQueue = (uPtr)dspRecvQ;
- itsDsppb->u.initParams.attnPtr = (uPtr)dspAttnBuf;
- itsDsppb->u.initParams.localSocket = *socket;
-
- itsCcb->refNum = 0;
-
- // make this connection
- FailOSErr(PBControl(itsDsppb, false));
-
- // remember the socket and refnum
- *socket = itsDsppb->u.initParams.localSocket;
- itsCcb->refNum = itsDsppb->ccbRefNum;
-
- initialized = true;
- }
-
-
-
-
- /***
- * DSPOptions
- *
- * Set the options for the conneciton
- *
- * zero for sendBlocking or badSeqMax will retain
- * their old values.
- *
- ***/
- void CADSP::DSPOptions(short sendBlocking, short badSeqMax, char useCheckSum)
- {
- ASSERT(initialized);
-
- // set up dspOptions parameters
- itsDsppb->ioCRefNum = itsDspRefNum;
- itsDsppb->csCode = dspOptions;
- itsDsppb->ccbRefNum = itsCcb->refNum;
-
- itsDsppb->u.optionParams.sendBlocking = sendBlocking;
- itsDsppb->u.optionParams.badSeqMax = badSeqMax;
- itsDsppb->u.optionParams.useCheckSum = useCheckSum;
-
- // set the options
- FailOSErr(PBControl(itsDsppb, false));
- }
-
-
-
-
- /***
- * DSPOpen
- *
- * Open a connection
- *
- * ocMode takes
- * ocPassive - for passive open
- * ocRequest - for active open
- *
- *
- * for Active open:
- * 'addr' contains the remote address
- *
- * for Passive open:
- * 'addr' contains the filter address
- * the call is made asynchronously
- * you can poll the ioResult until
- * a connection is made.
- *
- * There are two other open types, ocAccept and ocEstablish.
- * ••• they could be implemented in the future
- ***/
- void CADSP::DSPOpen(AddrBlock addr, unsigned char ocMode)
- {
- OSErr err;
- Boolean asynchronous = (ocMode == ocPassive);
-
- ASSERT(initialized);
-
- if (opened)
- {
- DSPClose();
- }
-
- // set up dspOpen parameters
- itsDsppb->ioCRefNum = itsDspRefNum;
- itsDsppb->csCode = dspOpen;
- itsDsppb->ccbRefNum = itsCcb->refNum;
-
- itsDsppb->u.openParams.remoteAddress = addr;
- itsDsppb->u.openParams.filterAddress = addr;
- itsDsppb->u.openParams.ocMode = ocMode;
- itsDsppb->u.openParams.ocInterval = 0;
- itsDsppb->u.openParams.ocMaximum = 0;
-
- // open the connection
- FailOSErr(PBControl(itsDsppb, asynchronous));
-
- opened = true;
- }
-
-
-
- /***
- * DSPClose
- *
- * Close the connection
- ***/
- void CADSP::DSPClose(void)
- {
- if (opened)
- {
- // set up dspClose parameters
- itsDsppb->ioCRefNum = itsDspRefNum;
- itsDsppb->csCode = dspClose;
- itsDsppb->ccbRefNum = itsCcb->refNum;
- itsDsppb->u.closeParams.abort = true;
-
- // do the close
- FailOSErr(PBControl(itsDsppb, false));
-
- opened = false;
- }
- }
-
-
-
-
- /***
- * DSPRemove
- *
- * Remove the connection completely
- * After this, the memory can be deallocated
- ***/
- void CADSP::DSPRemove(void)
- {
- if (opened)
- {
- // set up dspClose parameters
- itsDsppb->ioCRefNum = itsDspRefNum;
- itsDsppb->csCode = dspRemove;
- itsDsppb->ccbRefNum = itsCcb->refNum;
- itsDsppb->u.closeParams.abort = true;
-
- FailOSErr(PBControl(itsDsppb, false));
-
- opened = false;
- initialized = false;
- }
- }
-
-
-
-
-
-
- /**********************************************************************
-
- Maintaining an ADSP connection
-
- **********************************************************************/
-
-
- /***
- * DSPStatus
- *
- * Get status information for the connection
- *
- * sendPending - bytes in the send queue not sent yet
- * sendAvail - amount of available space in send queue
- * recvPending - bytes in the receive queue not read yet
- * recvAvail - amount of available space in receive queue
- *
- ***/
- void CADSP::DSPStatus(short *sendPending, short *sendAvail,
- short *recvPending, short *recvAvail)
- {
- ASSERT(opened);
-
- // setup the dspStatus parameters
- itsDsppb->ioCRefNum = itsDspRefNum;
- itsDsppb->csCode = dspStatus;
- itsDsppb->ccbRefNum = itsCcb->refNum;
-
- // get the status
- FailOSErr(PBControl(itsDsppb, false));
-
- *sendPending = itsDsppb->u.statusParams.sendQPending;
- *sendAvail = itsDsppb->u.statusParams.sendQFree;
- *recvPending = itsDsppb->u.statusParams.recvQPending;
- *recvAvail = itsDsppb->u.statusParams.recvQFree;
- }
-
-
-
-
-
- /***
- * DSPRead
- *
- * Read some data from the connection
- ***/
- void CADSP::DSPRead(void *buffer, short amountToRead, short *amountRead)
- {
- ASSERT(opened);
-
- // set up dspRead parameters
- itsDsppb->ioCRefNum = itsDspRefNum;
- itsDsppb->csCode = dspRead;
- itsDsppb->ccbRefNum = itsCcb->refNum;
-
- itsDsppb->u.ioParams.reqCount = amountToRead;
- itsDsppb->u.ioParams.dataPtr = (uPtr)buffer;
-
- // read from the connection
- FailOSErr(PBControl(itsDsppb, false));
-
- *amountRead = itsDsppb->u.ioParams.actCount;
- }
-
-
-
-
-
- /***
- * DSPWrite
- *
- * Write some stuff to the connection
- *
- ***/
- void CADSP::DSPWrite(void *buffer, short amountToWrite)
- {
- ASSERT(opened);
-
- // set up dspWrite parameters
- itsDsppb->ioCRefNum = itsDspRefNum;
- itsDsppb->csCode = dspWrite;
- itsDsppb->ccbRefNum = itsCcb->refNum;
-
- itsDsppb->u.ioParams.reqCount = amountToWrite;
- itsDsppb->u.ioParams.dataPtr = (uPtr)buffer;
- itsDsppb->u.ioParams.eom = 1;
- itsDsppb->u.ioParams.flush = 1;
-
- // write to the connection
- FailOSErr(PBControl(itsDsppb, false));
- }
-